﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;
    public class ScoreMoneyAwards
    {
        public int Add(Chain.Model.ScoreMoneyAwards model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into ScoreMoneyAwards(");
            builder.Append("Nums,state,Status,isdel)");
            builder.Append(" values (");
            builder.Append("@Nums,@state,@Status,@isdel)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@Nums", SqlDbType.Money, 8),
                new SqlParameter("@state", SqlDbType.Int, 4),
                new SqlParameter("@Status",SqlDbType.Int, 4),
                new SqlParameter("@isdel", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.Nums;
            cmdParms[1].Value = model.state;
            cmdParms[2].Value = model.Status;
            cmdParms[3].Value = model.isdel;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from ScoreMoneyAwards ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
        public bool IsDelete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update ScoreMoneyAwards set isdel=1");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from ScoreMoneyAwards");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }
        public bool Update(Chain.Model.ScoreMoneyAwards model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update ScoreMoneyAwards set ");
            builder.Append("Nums=@Nums,");
            builder.Append("state=@state,");
            builder.Append("Status=@Status,");
            builder.Append("isdel=@isdel");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                    new SqlParameter("@Nums", SqlDbType.Money, 8),
                new SqlParameter("@state", SqlDbType.Int, 4),
                new SqlParameter("@Status",SqlDbType.Int, 4),
                new SqlParameter("@isdel", SqlDbType.Int, 4),
                new SqlParameter("@ID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.Nums;
            cmdParms[1].Value = model.state;
            cmdParms[2].Value = model.Status;
            cmdParms[3].Value = model.isdel;
            cmdParms[4].Value = model.ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
        public DataSet GetItem(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ID,Nums,state,Status,isdel from ScoreMoneyAwards ");
            builder.Append(" where ID=@ID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms);
        }
        public Chain.Model.ScoreMoneyAwards GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 *");
            builder.Append(" from ScoreMoneyAwards");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }
        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select *");
            builder.Append(" FROM ScoreMoneyAwards ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" * ");
            builder.Append(" FROM ScoreMoneyAwards ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }
        public DataSet GetScoreMoneyAwardsInfo(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "ScoreMoneyAwards";
            string[] column = new string[] { "ID,Nums,state,Status" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }
        public Chain.Model.ScoreMoneyAwards DataRowToModel(DataRow row)
        {
            Chain.Model.ScoreMoneyAwards model = new Chain.Model.ScoreMoneyAwards();
            if (row != null)
            {
                if ((row["ID"] != null) && (row["ID"].ToString() != ""))
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if ((row["Nums"] != null) && (row["Nums"].ToString() != ""))
                {
                    model.Nums = decimal.Parse(row["Nums"].ToString());
                }
                if ((row["state"] != null) && (row["state"].ToString() != ""))
                {
                    model.state = int.Parse(row["state"].ToString());
                }
                if ((row["Status"] != null) && (row["Status"].ToString() != ""))
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["isdel"] != null && (row["isdel"].ToString() != ""))
                {
                    model.isdel = int.Parse(row["isdel"].ToString());
                }
            }
            return model;
        }
    }
}
